package com.maixy.studyonlineapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maixy.studyonlineapi.entity.Activity;
import com.maixy.studyonlineapi.entity.Auth;
import com.maixy.studyonlineapi.entity.UserAbility;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.mapper.UserAbilityMapper;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.UserAbilityView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName UserAbilityService
 * @Description TODO
 * @Date 2020/11/29 14:46
 * @Author maixy
 */
@Service
public class UserAbilityService extends BaseMPlusService<UserAbility, Long>{
    private final UserAbilityMapper userAbilityMapper;

    @Resource
    private ActivityService activityService;

    @Resource
    private AuthService authService;

    protected UserAbilityService(UserAbilityMapper userAbilityMapper) {
        super(UserAbility.class, userAbilityMapper);
        this.userAbilityMapper = userAbilityMapper;
    }

    /**
     * 保存用户对课程的能力
     *
     * @param activityId 活动id
     * @param contentLevel 难度
     * @param contentType 类型
     * @param userId 当前用户id
     */
    public void saveOrUpdateAbility(Long activityId, Integer contentLevel, Integer contentType, Long userId) {
        Activity activity = activityService.findById(activityId);
        if (null == activity || activity.getHasDelete()) {
            throw new TipException("课程不存在或以被删除");
        }
        UserAbility userAbility = findUserAbilityBuUserIdAndActivityId(userId, activityId);
        if (null == userAbility || userAbility.getHasDelete()) {
            userAbility = new UserAbility();
            userAbility.setActivityId(activityId);
            userAbility.setContentLevel(contentLevel);
            userAbility.setContentType(contentType);
            userAbility.setCreateBy(userId);
            userAbility.setCreateTime(System.currentTimeMillis());
            userAbility.setHasDelete(false);
            create(userAbility);
        } else {
            userAbility.setContentLevel(contentLevel);
            userAbility.setContentType(contentType);
            userAbility.setUpdateBy(userId);
            userAbility.setUpdateTime(System.currentTimeMillis());
            update(userAbility);
        }
    }

    public UserAbilityView abilityDetail(Long activityId, Long userId) {
        //标签视图
        UserAbility userAbility = findUserAbilityBuUserIdAndActivityId(userId, activityId);
        if (null == userAbility || userAbility.getHasDelete()) {
            throw new TipException("找不到您在这门课程中的标签或已被删除");
        }
        return convert(userAbility);
    }

    /**
     * 学员在课程中的标签视图转换
     *
     * @param userAbility 学员标签视图
     * @return com.maixy.studyonlineapi.view.UserAbilityView
     */
    private UserAbilityView convert(UserAbility userAbility) {
        UserAbilityView view = new UserAbilityView();
        view.setActivityId(DataTypeUtil.getStr(userAbility.getActivityId()));
        view.setContentLevel(userAbility.getContentLevel());
        view.setContentType(userAbility.getContentType());
        return view;
    }

    /**
     * 判断某个人是否在某个课中填写自己的能力
     *
     * @param activityId 活动id
     * @param userId 当前用户id
     * @return java.lang.Boolean
     */
    public Boolean getHasAbility(Long activityId, Long userId) {
        Activity activity = activityService.findById(activityId);
        if (null == activity || activity.getHasDelete()) {
            throw new TipException("活动不存在或已被删除");
        }
        Auth auth = authService.findAuthByUserIdAndActivityId(userId, activityId);
        if (null == auth || auth.getHasDelete()) {
            throw new TipException("您还未报名！！！");
        }
        UserAbility userAbility = findUserAbilityBuUserIdAndActivityId(userId, activityId);
        return null != userAbility && !userAbility.getHasDelete();
    }

    /**
     * 查找某个用户对某门课的能力
     *
     * @param userId 当前用户id
     * @param activityId 活动id
     * @return com.maixy.studyonlineapi.entity.UserAbility
     */
    public UserAbility findUserAbilityBuUserIdAndActivityId(Long userId, Long activityId) {
        return userAbilityMapper.selectOne(new QueryWrapper<UserAbility>().lambda()
                .eq(UserAbility::getCreateBy, userId)
                .eq(UserAbility::getActivityId, activityId)
                .eq(UserAbility::getHasDelete, false)
                .orderByDesc(UserAbility::getCreateTime)
                .last("limit 1"));
    }
}
